package com.orderSystem;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import com.vo.DispensingVo;
import org.apache.commons.lang.ObjectUtils.Null;
import com.databaseManager.*;
import com.mysql.fabric.Response;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import net.sf.json.JSONObject;

/**
 * 代写的部分放在这里了，避免错乱
 */
public class HandlerJri {


    /**
     * URL:/get/dispensing
     */
    static class getDispensingHandler implements HttpHandler {

        @Override
        public void handle(HttpExchange arg0) throws IOException {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        DatabaseFunc2 df = new DatabaseFunc2();
                        String queryString = arg0.getRequestURI().getQuery();
                        /* 将获取到的报文体转换为键值对 */
                        Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
                        arg0.sendResponseHeaders(200, 0);
                        /**
                         * 获取请求数据
                         */
                        String id=queryStringInfo.get("ID");
                        /**
                         * 进行查询
                         */
                        DispensingVo dispensingMsg=df.getDispensingMsg(id);
                        JSONObject jsonObject = JSONObject.fromObject(dispensingMsg);
                        OutputStream os = arg0.getResponseBody();
                        String response = jsonObject.toString();
                        os.write(response.getBytes());
                        os.close();
                    } catch (IOException ie) {
                        ie.printStackTrace();
                        // TODO Auto-generated catch block
                    } catch (Exception e) {
                        e.printStackTrace();
                        // TODO: handle exception
                    }
                    // TODO Auto-generated method stub
                }
            }).start();
            // TODO Auto-generated method stub
        }
    }
    /**
     * URL:/get/progress
     */
    static class getprogress implements HttpHandler {

        @Override
        public void handle(HttpExchange arg0) throws IOException {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        DatabaseFunc2 df = new DatabaseFunc2();
                        String queryString = arg0.getRequestURI().getQuery();
                        /* 将获取到的报文体转换为键值对 */
                        Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
                        arg0.sendResponseHeaders(200, 0);
                        /**
                         * 获取请求数据
                         */
                        String id=queryStringInfo.get("ID");
                        /**
                         * 进行查询
                         */
                        String dispensingMsg=df.getProgressState(id);
                        JSONObject jsonObject = JSONObject.fromObject(dispensingMsg);
                        OutputStream os = arg0.getResponseBody();
                        String response = jsonObject.toString();
                        os.write(response.getBytes());
                        os.close();
                    } catch (IOException ie) {
                        ie.printStackTrace();
                        // TODO Auto-generated catch block
                    } catch (Exception e) {
                        e.printStackTrace();
                        // TODO: handle exception
                    }
                    // TODO Auto-generated method stub
                }
            }).start();
            // TODO Auto-generated method stub
        }
    }

    /**
     * ================================  药房  ==================================================
     */
    /**
     * URL:/post/dispensing
     */
    static class dispensing implements HttpHandler {

        @Override
        public void handle(HttpExchange arg0) throws IOException {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        DatabaseFunc2 df = new DatabaseFunc2();
                        String queryString = arg0.getRequestURI().getQuery();
                        int state=0;
                        /* 将获取到的报文体转换为键值对 */
                        Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
                        arg0.sendResponseHeaders(200, 0);
                        /**
                         * 获取请求数据
                         */
                        String diagId=queryStringInfo.get("prescriptionNum");
                        /**
                         * 配药
                         */
                        try {
                            df.dispensing(diagId);
                            state=1;
                        }catch (Exception e){
                            e.printStackTrace();
                            state=0;
                        }
                        JSONObject jsonObject = JSONObject.fromObject(state);
                        OutputStream os = arg0.getResponseBody();
                        String response = jsonObject.toString();
                        os.write(response.getBytes());
                        os.close();
                    } catch (IOException ie) {
                        ie.printStackTrace();
                        // TODO Auto-generated catch block
                    } catch (Exception e) {
                        e.printStackTrace();
                        // TODO: handle exception
                    }
                    // TODO Auto-generated method stub
                }
            }).start();
            // TODO Auto-generated method stub
        }
    }
    /**
     * URL:/post/confirmMedi
     */
    static class confirmMedi implements HttpHandler {

        @Override
        public void handle(HttpExchange arg0) throws IOException {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        DatabaseFunc2 df = new DatabaseFunc2();
                        String queryString = arg0.getRequestURI().getQuery();
                        int state=0;
                        /* 将获取到的报文体转换为键值对 */
                        Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
                        arg0.sendResponseHeaders(200, 0);
                        /**
                         * 获取请求数据
                         */
                        String diagId=queryStringInfo.get("prescriptionNum");
                        /**
                         * 配药
                         */
                        try {
                            df.comfirmMedi(diagId);
                            state=1;
                        }catch (Exception e){
                            e.printStackTrace();
                            state=0;
                        }
                        JSONObject jsonObject = JSONObject.fromObject(state);
                        OutputStream os = arg0.getResponseBody();
                        String response = jsonObject.toString();
                        os.write(response.getBytes());
                        os.close();
                    } catch (IOException ie) {
                        ie.printStackTrace();
                        // TODO Auto-generated catch block
                    } catch (Exception e) {
                        e.printStackTrace();
                        // TODO: handle exception
                    }
                    // TODO Auto-generated method stub
                }
            }).start();
            // TODO Auto-generated method stub
        }
    }
}
